/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules.NavigationProvider;
using DotNetNuke.UI.WebControls;

using DotNetNuke.Modules.Articles.Common;
using Constants = DotNetNuke.Modules.Articles.Common.Constants;
using DotNetNuke.Modules.Articles.Framework;

#endregion

namespace DotNetNuke.Modules.Articles.UI
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The CollectionSkinObjectBase base class provides standard properties for the
    /// skin objects that expose collections of items
    /// </summary>
    /// <history>
    /// 	[cnurse]	03/31/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class CollectionSkinObjectBase : ArticleSkinObjectBase
    {
        #region Private Members

        private int _StartIndex = 0;

        private string _AddButtonCaption;
        private string _AddButtonKey;
        private string _Caption;
        private string _CaptionKey;
        private string _EditButtonKey;
        private string _EditButtonTooltip;
        private string _ViewButtonKey;
        private string _ViewButtonTooltip;

        private Style _AddButtonStyle;
        private Style _CaptionStyle;
        private Style _EditButtonStyle;
        private Style _ViewButtonStyle;
        

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The text of the Add button (to be used if no resource key is specified)
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The add button Caption.")]
        public string AddButtonCaption
        {
            get { return _AddButtonCaption; }
            set { _AddButtonCaption = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the Add Button
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/06/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the Add Button")]
        public string AddButtonKey
        {
            get { return _AddButtonKey; }
            set { _AddButtonKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The text of the caption (to be used if no resource key is specified)
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The control Caption.")]
        public string Caption
        {
            get { return _Caption; }
            set { _Caption = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the Title
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/06/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localizae the Caption")]
        public string CaptionKey
        {
            get { return _CaptionKey; }
            set { _CaptionKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the Edit Button
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/30/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the Edit Button")]
        public string EditButtonKey
        {
            get { return _EditButtonKey; }
            set { _EditButtonKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The tooltip of the Edit button (to be used if no resource key is specified)
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/30/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The edit button Tooltip.")]
        public string EditButtonTooltip
        {
            get { return _EditButtonTooltip; }
            set { _EditButtonTooltip = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A flag that indicates whether the control has an add Command Button.
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Behavior"),
        DefaultValue(true),
        Description("Optionally display an Add Command Button to users with Edit permissions.")]
        public bool ShowAdd
        {
            get
            {
                object b = ViewState["ShowAdd"];
                return (b == null) ? true : (bool)b;
            }
            set { ViewState["ShowAdd"] = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets whether to display the Title
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/21/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(true),
        Description("Set whether to display the title.")]
        public bool ShowCaption
        {
            get
            {
                object b = ViewState["ShowCaption"];
                return (b == null) ? true : (bool)b;
            }
            set
            {
                ViewState["ShowCaption"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A flag that indicates whether the control has an edit "pencil" displayed.
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Behavior"),
        DefaultValue(true),
        Description("Optionally display the Edit pencil to a user with Edit permissions.")]
        public bool ShowEdit
        {
            get
            {
                object b = ViewState["ShowEdit"];
                return (b == null) ? true : (bool)b;
            }
            set { ViewState["ShowEdit"] = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The first issue in the collection to display
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(0),
        Description("You can configure the Navigation provider's properties.")]
        public int StartIndex
        {
            get { return _StartIndex; }
            set { _StartIndex = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the View Button
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/19/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the View Button")]
        public string ViewButtonKey
        {
            get { return _ViewButtonKey; }
            set { _ViewButtonKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The tooltip of the View button (to be used if no resource key is specified)
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/19/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The view button Tooltip.")]
        public string ViewButtonTooltip
        {
            get { return _ViewButtonTooltip; }
            set { _ViewButtonTooltip = value; }
        }

        #region Style Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the Add Button
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Style"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the Add Button.")]
        public Style AddButtonStyle
        {
            get
            {
                if (_AddButtonStyle == null)
                    _AddButtonStyle = new Style();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_AddButtonStyle).TrackViewState();
                }
                return _AddButtonStyle;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the caption
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Style"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the caption text.")]
        public Style CaptionStyle
        {
            get
            {
                if (_CaptionStyle == null)
                    _CaptionStyle = new Style();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_CaptionStyle).TrackViewState();
                }
                return _CaptionStyle;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the Edit Button
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/20/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Style"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the Edit Button.")]
        public Style EditButtonStyle
        {
            get
            {
                if (_EditButtonStyle == null)
                    _EditButtonStyle = new Style();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_EditButtonStyle).TrackViewState();
                }
                return _EditButtonStyle;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the View Button
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/19/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Style"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the View Button.")]
        public Style ViewButtonStyle
        {
            get
            {
                if (_ViewButtonStyle == null)
                    _ViewButtonStyle = new Style();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_ViewButtonStyle).TrackViewState();
                }
                return _ViewButtonStyle;
            }
        }

        #endregion

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderAddButton renders the control's add button
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <param name="includeBreak">A flag that determines whether a break tag is rendered
        /// before the button is rendered.</param>
        /// <history>
        /// 	[cnurse]	03/31/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected virtual void RenderAddButton(HtmlTextWriter writer, string url, bool includeBreak)
        {
            bool _ShowAdd = false;
            if (DesignMode)
                _ShowAdd = true;
            else
                _ShowAdd = ShowAdd && (ParentModule != null) && ParentModule.IsEditable;

            if (_ShowAdd)
            {
                string addButtonText = String.Empty;
                if (String.IsNullOrEmpty(AddButtonKey) || DesignMode)
                    addButtonText = AddButtonCaption;
                else
                    addButtonText = LocalizeString(AddButtonKey);

                //Render <br/>
                if (includeBreak)
                    writer.WriteBreak();

                if (AddButtonStyle != null)
                    AddButtonStyle.AddAttributesToRender(writer);

                //Render Hyperlink
                writer.AddAttribute(HtmlTextWriterAttribute.Href, url);
                writer.AddAttribute(HtmlTextWriterAttribute.Title, addButtonText);
                writer.RenderBeginTag(HtmlTextWriterTag.A);

                //Render Image
                writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(Constants.IMAGE_Add));
                writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);

                //Render end of Image
                writer.RenderEndTag();

                //Render Hyperlink Text
                writer.Write(addButtonText);

                //Render end of Hyperlink
                writer.RenderEndTag();
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderCaption renders the control's caption
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	03/31/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected virtual void RenderCaption(HtmlTextWriter writer)
        {
            //Render the Caption
            if (ShowCaption)
            {
                if (CaptionStyle != null)
                    CaptionStyle.AddAttributesToRender(writer);

                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                if (String.IsNullOrEmpty(CaptionKey) || DesignMode)
                    writer.Write(Caption);
                else
                    writer.Write(LocalizeString(CaptionKey));
                writer.RenderEndTag();
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderEditButton renders the control's edit button
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	03/31/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected virtual void RenderEditButton(HtmlTextWriter writer, string navigateUrl)
        {
            bool _ShowEdit = false;
            if (DesignMode)
                _ShowEdit = true;
            else
                _ShowEdit = ShowEdit && (ParentModule != null) && ParentModule.IsEditable;

            if (_ShowEdit)
            {
                string editButtonText = String.Empty;
                if (String.IsNullOrEmpty(EditButtonKey) || DesignMode)
                    editButtonText = EditButtonTooltip;
                else
                    editButtonText = LocalizeString(EditButtonKey);

                if (EditButtonStyle != null)
                    EditButtonStyle.AddAttributesToRender(writer);

                //Render Hyperlink
                writer.AddAttribute(HtmlTextWriterAttribute.Href, navigateUrl);
                writer.AddAttribute(HtmlTextWriterAttribute.Title, editButtonText);
                writer.RenderBeginTag(HtmlTextWriterTag.A);

                //Render Image
                writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(Constants.IMAGE_Edit));
                writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);

                //Render end of Image
                writer.RenderEndTag();

                //Render end of Hyperlink
                writer.RenderEndTag();
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderViewButton renders the control's view button
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	07/19/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected virtual void RenderViewButton(HtmlTextWriter writer, string navigateUrl)
        {
            bool _ShowEdit = false;
            if (DesignMode)
                _ShowEdit = true;
            else
                _ShowEdit = ShowEdit && (ParentModule != null) && ParentModule.IsEditable;

            if (_ShowEdit)
            {
                string viewButtonText = String.Empty;
                if (String.IsNullOrEmpty(EditButtonKey) || DesignMode)
                    viewButtonText = ViewButtonTooltip;
                else
                    viewButtonText = LocalizeString(ViewButtonKey);

                if (ViewButtonStyle != null)
                    ViewButtonStyle.AddAttributesToRender(writer);

                //Render Hyperlink
                writer.AddAttribute(HtmlTextWriterAttribute.Href, navigateUrl);
                writer.AddAttribute(HtmlTextWriterAttribute.Title, viewButtonText);
                writer.RenderBeginTag(HtmlTextWriterTag.A);

                //Render Image
                writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(Constants.IMAGE_View));
                writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);

                //Render end of Image
                writer.RenderEndTag();

                //Render end of Hyperlink
                writer.RenderEndTag();
            }
        }

#endregion
    
    }
}
